package com.iwomy.secureplat.platform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.data.datascope.DataScope;
import com.iwomy.secureplat.platform.dto.BasicPlaceBuildingDTO;
import com.iwomy.secureplat.platform.entity.BasicInstallLocationEntity;
import com.iwomy.secureplat.platform.entity.BasicPlaceBuildingEntity;
import com.iwomy.secureplat.platform.entity.BasicPlaceFloorEntity;
import com.iwomy.secureplat.platform.utils.DeptFillUtils;
import com.iwomy.secureplat.platform.utils.RemoteDeptUtils;
import com.iwomy.secureplat.platform.mapper.BasicPlaceBuildingMapper;
import com.iwomy.secureplat.platform.service.BasicInstallLocationService;
import com.iwomy.secureplat.platform.service.BasicPlaceBuildingService;
import com.iwomy.secureplat.platform.service.BasicPlaceFloorService;
import com.iwomy.secureplat.platform.vo.BasicPlaceBuildingVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 建筑物管理
 *
 * @author pig
 * @date 2023-11-30 13:54:55
 */
@Service
public class BasicPlaceBuildingServiceImpl extends ServiceImpl<BasicPlaceBuildingMapper, BasicPlaceBuildingEntity> implements BasicPlaceBuildingService {

	@Autowired
	private BasicPlaceFloorService basicPlaceFloorService;

	@Autowired
	private BasicInstallLocationService basicInstallLocationService;

	@Autowired
	private RemoteDeptUtils remoteDeptUtils;

	@Override
	public Page<BasicPlaceBuildingVO> getBasicPlaceBuildingPage(Page page, BasicPlaceBuildingDTO basicPlaceBuildingDTO) {
		LambdaQueryWrapper<BasicPlaceBuildingEntity> queryWrapper = Wrappers.lambdaQuery();
		List<BasicPlaceBuildingEntity> list = this.baseMapper.selectListByScope(queryWrapper, new DataScope());
		List<Long> id = list.stream().map(BasicPlaceBuildingEntity::getId).collect(Collectors.toList());

		if (ObjectUtil.isEmpty(id)) return page;

		LambdaQueryWrapper<BasicPlaceBuildingEntity> wrapper = Wrappers.lambdaQuery();

		if (ObjectUtil.isNotEmpty(basicPlaceBuildingDTO.getDeptId())) {
			wrapper.in(BasicPlaceBuildingEntity::getDeptId, basicPlaceBuildingDTO.getDeptId());
		}

		if (ObjectUtil.isNotEmpty(basicPlaceBuildingDTO.getBuildingId())) {
			wrapper.in(BasicPlaceBuildingEntity::getId, basicPlaceBuildingDTO.getBuildingId());
		}

		if (StrUtil.isNotEmpty(basicPlaceBuildingDTO.getDetailAddress())) {
			wrapper.like(BasicPlaceBuildingEntity::getDetailAddress, basicPlaceBuildingDTO.getDetailAddress());
		}

		wrapper.in(BasicPlaceBuildingEntity::getId, id);

		Page<BasicPlaceBuildingEntity> basicPlaceBuildingPage = baseMapper.selectPage(page, wrapper);
		List<BasicPlaceBuildingEntity> records = basicPlaceBuildingPage.getRecords();
		List<BasicPlaceBuildingVO> basicPlaceBuildingVOS = new ArrayList<>();
		for (BasicPlaceBuildingEntity record : records) {
			BasicPlaceBuildingVO vo = new BasicPlaceBuildingVO();
			BeanUtils.copyProperties(record, vo); // 将 Entity 对象的属性复制到 VO 对象

			Long buildingId = record.getId();
			int floorCount = (int) basicPlaceFloorService.count(new QueryWrapper<BasicPlaceFloorEntity>()
					.eq("building_id", buildingId));
			vo.setBoundFloorCount(floorCount);
			record.setBoundFloorCount(floorCount);

			int installCount = (int) basicInstallLocationService.count(new QueryWrapper<BasicInstallLocationEntity>()
					.eq("building_id", buildingId));
			vo.setBoundInstallCount(installCount);
			record.setBoundInstallCount(installCount);

			basicPlaceBuildingVOS.add(vo);
			this.baseMapper.updateById(record);
		}
		Page<BasicPlaceBuildingVO> voPage = new Page<>(basicPlaceBuildingPage.getCurrent(), basicPlaceBuildingPage.getSize());
		voPage.setTotal(basicPlaceBuildingPage.getTotal());
		voPage.setRecords(basicPlaceBuildingVOS);
		DeptFillUtils.fill(voPage);
		return voPage;
	}

	@Override
	public BasicPlaceBuildingVO getById(Long id) {
		BasicPlaceBuildingEntity basicPlaceBuilding = this.getBaseMapper().selectById(id);

		BasicPlaceBuildingVO basicPlaceBuildingVO = new BasicPlaceBuildingVO();
		BeanUtils.copyProperties(basicPlaceBuilding, basicPlaceBuildingVO);

		Long deptId = basicPlaceBuilding.getDeptId();
		if (deptId != null) {
			String deptName = remoteDeptUtils.getDeptName(deptId);
			basicPlaceBuildingVO.setDeptName(deptName);
		}
		int installCount = (int) basicInstallLocationService.count(new QueryWrapper<BasicInstallLocationEntity>()
				.eq("building_id", id));
		basicPlaceBuildingVO.setBoundInstallCount(installCount);
		return basicPlaceBuildingVO;
	}

	@Override
	public R newlyAdded(BasicPlaceBuildingEntity basicPlaceBuilding) {
		this.save(basicPlaceBuilding);
		basicPlaceBuilding.getBoundFloorCount();
		int boundFloorCount = basicPlaceBuilding.getBoundFloorCount();
		List<BasicPlaceFloorEntity> floorList = new ArrayList<>();
		for (int i = 1; i <= boundFloorCount; i++) {
			BasicPlaceFloorEntity floor = new BasicPlaceFloorEntity();
			floor.setFloorName(i+"楼");
			floor.setBuildingId(basicPlaceBuilding.getId());
			floorList.add(floor);
		}
		if (!floorList.isEmpty()) {
			basicPlaceFloorService.saveBatch(floorList);
		}

		return R.ok();
	}

	@Override
	public List<BasicPlaceBuildingEntity> pageByScope(BasicPlaceBuildingEntity basicPlaceBuilding) {
		List<BasicPlaceBuildingEntity> basicPlaceBuildingEntities = this.getBaseMapper().selectListByScope(Wrappers.lambdaQuery(basicPlaceBuilding), new DataScope());
		return basicPlaceBuildingEntities;
	}


}